﻿#include "Display.h"

void Display_t::Init( uint16_t xSize, uint16_t ySize )
{
    bool texFilter = false;
    m_PresentEnable = false;
    
    m_ScreenWidth  = xSize;
    m_ScreenHeight = ySize;



    int rc = SDL_Init( SDL_INIT_VIDEO );
    //assert(rc == 0);
   
    SDL_Surface* surf = SDL_SetVideoMode(m_ScreenWidth, m_ScreenHeight, 0, SDL_OPENGL | SDL_GL_DOUBLEBUFFER| SDL_RESIZABLE );
    //assert(surf != NULL);


    glViewport(0, 0, m_ScreenWidth, m_ScreenHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, m_ScreenWidth, m_ScreenHeight, 0, 1, -1);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &m_TexId);
    glBindTexture(GL_TEXTURE_2D, m_TexId);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, DISPLAY_HORIZONTAL_RESOLUTION, DISPLAY_PPU_VERTICAL_PAL_RESOLUTION, 0, GL_RGB, GL_UNSIGNED_BYTE, m_pFrameBuffer);
 
   
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texFilter ? GL_LINEAR : GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}


void Display_t::Run( void )
{ 
    
    if( m_PresentEnable )
    {
        m_PresentEnable = false;
        glClearColor(1, 0, 0, 0);
        glClear(GL_COLOR_BUFFER_BIT);
 
 
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, m_TexId);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, DISPLAY_HORIZONTAL_RESOLUTION, DISPLAY_PPU_VERTICAL_PAL_RESOLUTION, GL_RGB, GL_UNSIGNED_BYTE, m_pFrameBuffer);
 
        glBegin( GL_QUADS );
            glTexCoord2d(0.0, 0.0);  glVertex2d(0.0, 0.0);
            glTexCoord2d(1.0, 0.0);  glVertex2d(m_ScreenWidth, 0.0);
            glTexCoord2d(1.0, 1.0);  glVertex2d(m_ScreenWidth, m_ScreenHeight);
            glTexCoord2d(0.0, 1.0);  glVertex2d(0.0, m_ScreenHeight);
        glEnd();
 
        SDL_GL_SwapBuffers();
    }
}


void Display_t::Present( _in_ DisplayIn_t* displayIn )
{
    m_pFrameBuffer = displayIn->pData;
    m_PresentEnable = true;
}


//void Display_t::SetWindowHandler( HWND windowHandler)
//{
//    m_TargetWindowHandler = windowHandler;
//}


//void Display_t::PresentScreenShot( _in_ DisplayIn_t* displayIn)
//{
//    HANDLE                hFile;
//    DWORD                numberOfWritedBytes;
//    BmpFileHeader_t        bmpFileHeader;
//    BmpFileInfoHeader_t bmpFileInfoHeader;
//    uint8_t                palette [BMP_PALETTE_SIZE];    // �������
//    LPCWSTR                screenShotFileName;
//    BOOL status = false;
//    uint32_t error = 0;
//
//    for(uint16_t i = 0; i !=BMP_PALETTE_SIZE; i++)
//    {
//        palette[i] = 0;
//    }
//        
//    bmpFileHeader.signature        = BMP_FILE_SIGNATURE;
//    bmpFileHeader.fileSize        = sizeof(BmpFileHeader_t) + sizeof(BmpFileInfoHeader_t) + BMP_PALETTE_SIZE + sizeof(PixelStruct) * displayIn->xSize * displayIn->ySize +  displayIn->ySize * (displayIn->xSize % 4); // ������ ��������� �����
//    bmpFileHeader.reserved        = 0;
//    bmpFileHeader.imageOffset    = sizeof(BmpFileHeader_t) + sizeof(BmpFileInfoHeader_t) + BMP_PALETTE_SIZE;
//
//    bmpFileInfoHeader.bmpFileInfoHeaderSize = sizeof(BmpFileInfoHeader_t);
//    bmpFileInfoHeader.imageWidth            = displayIn->xSize;
//    bmpFileInfoHeader.imageHeight            = - displayIn->ySize;
//    bmpFileInfoHeader.planesAmount            = 1;
//    bmpFileInfoHeader.bitPerPixel            = displayIn->bitPerPixel;
//    bmpFileInfoHeader.comressionType        = BMP_RGB_COMPRESSION_TYPE;
//    bmpFileInfoHeader.comressedImageSize    = 0;
//    bmpFileInfoHeader.xPixelPerMeter        = 0;
//    bmpFileInfoHeader.yPixelPerMeter        = 0;
//    bmpFileInfoHeader.colorAmount            = 0;
//    bmpFileInfoHeader.colorImpotantAmount    = 0;
//    
//    //screenShotFileName = L"D:\\display.bmp";
//    screenShotFileName = L"display.bmp";
//    
//    
//    do
//    {
//        hFile = CreateFileW (screenShotFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
//        if (hFile == INVALID_HANDLE_VALUE)
//        {
//            error = GetLastError();
//            break;
//        }
//
//        status = WriteFile (hFile, &bmpFileHeader, sizeof (BmpFileHeader_t), &numberOfWritedBytes, NULL);// ������ ��������� �����
//        if( false == status )
//        {
//            error = GetLastError();
//            break;
//        }
//    
//        status = WriteFile (hFile, &bmpFileInfoHeader, sizeof (BmpFileInfoHeader_t), &numberOfWritedBytes, NULL); //������ ���������� �� �����������
//        if( false == status )
//        {
//            error = GetLastError();
//            break;
//        }
//    
//
//        status = WriteFile (hFile, palette, BMP_PALETTE_SIZE, &numberOfWritedBytes, NULL); // ������ �������
//        if( false == status )
//        {
//            error = GetLastError();
//            break;
//        }
//    
//
//        for (uint16_t i = 0; i < displayIn->ySize; i++)
//        {
//            status = WriteFile(hFile, displayIn->pData, sizeof(PixelStruct) * displayIn->xSize , &numberOfWritedBytes, NULL); //������ ������ ��������
//            if( false == status )
//            {
//                error = GetLastError();
//                break;
//            }
//        
//            status = WriteFile(hFile, palette, displayIn->xSize % 4, &numberOfWritedBytes, NULL); // ��������� �� �������
//            if( false == status )
//            {
//                error = GetLastError();
//                break;
//            }
//    
//            displayIn->pData += displayIn->xSize;
//        }
//    }while( false );
//
//    CloseHandle(hFile);
//}


